package com.yingxin.yxerp.module.activiti.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.activiti.def.*;
import com.yingxin.yx.framework.commons.activiti.enums.FormKey;
import com.yingxin.yxerp.module.activiti.entity.ActivitiForm;
import com.yingxin.yxerp.module.activiti.mapper.ActivitiFormMapper;
import com.yingxin.yxerp.module.activiti.service.IActivitiFormService;
import com.yingxin.yxerp.module.activiti.vo.req.ActivitiFormQueryVo;
import com.yingxin.yxerp.module.activiti.vo.resp.ActivitiFormRespVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 表单 服务实现类
 * @author fanchao
 * @since 2021-10-18
 */

@Service
public class ActivitiFormServiceImpl extends ServiceImpl<ActivitiFormMapper, ActivitiForm> implements IActivitiFormService {

    /**
     * 分页查询
     */
    @Override
    public IPage<ActivitiFormRespVo> pageVo(Page page, ActivitiFormQueryVo queryVo) {
        return baseMapper.pageVo(page, queryVo);
    }

    /**
     * 查询全部
     */
    @Override
    public List<ActivitiFormRespVo> listVo(ActivitiFormQueryVo queryVo) {
        return baseMapper.listVo(queryVo);
    }

    /**
     * getVo
     */
    @Override
    public ActivitiFormRespVo getVo(String id) {
        ActivitiFormRespVo form = baseMapper.getVo(id);
        // approvalFormMeta
        form.setApprovalFormMetaList(JSON.parseArray(form.getApprovalFormMetaContent(), ApprovalFormMeta.class));
        disposePrintLimit(form);
        addIncomeSubtract(form);
        // conditionMeta
        form.setConditionMetaList(JSON.parseArray(form.getConditionMetaContent(), ConditionMeta.class));
        // actionMeta
        disposeActionMeta(form);
        form.setTasks(JSON.parseArray(form.getBpmnTask(), TaskDef.class));
        form.setConditionExpressions(JSON.parseArray(form.getBpmnConditionExpression(), ConditionExpression.class));
        return form;
    }

    /**
     * 打印限制
     */
    private void disposePrintLimit(ActivitiFormRespVo form) {
        if (form.getApprovalFormMetaList() == null) {
            form.setApprovalFormMetaList(new ArrayList<>());
        }
        ApprovalFormMeta formMeta = new ApprovalFormMeta(FormKey.PRINT_LIMIT.getKey(), FormKey.PRINT_LIMIT.getKeyName());
        form.getApprovalFormMetaList().add(formMeta);
    }

    /**
     * 进项减少限制
     * @param form
     */
    private void addIncomeSubtract(ActivitiFormRespVo form){
        if (form.getApprovalFormMetaList() == null) {
            form.setApprovalFormMetaList(new ArrayList<>());
        }
        ApprovalFormMeta formMeta = new ApprovalFormMeta(FormKey.INCOME_SUBTRACT.getKey(), FormKey.INCOME_SUBTRACT.getKeyName());
        form.getApprovalFormMetaList().add(formMeta);
    }

    /**
     * 按钮
     */
    private void disposeActionMeta(ActivitiFormRespVo form) {
        List<TaskActionDef> actionMetaList = new ArrayList<>();
        TaskActionDef approved = new TaskActionDef();
        approved.setId("A");
        approved.setDefaultLabel("通过");
        approved.setLabel("通过");
        approved.setValue("A");
        approved.setDisabled(true);
        actionMetaList.add(approved);
        TaskActionDef finalApproved = new TaskActionDef();
        finalApproved.setId("FA");
        finalApproved.setDefaultLabel("通过并结束流程");
        finalApproved.setLabel("通过并结束流程");
        finalApproved.setValue("FA");
        finalApproved.setDisabled(false);
        actionMetaList.add(finalApproved);
        TaskActionDef unapproved = new TaskActionDef();
        unapproved.setId("R");
        unapproved.setDefaultLabel("拒绝");
        unapproved.setLabel("拒绝");
        unapproved.setValue("R");
        unapproved.setDisabled(false);
        actionMetaList.add(unapproved);
        TaskActionDef transfer = new TaskActionDef();
        transfer.setId("T");
        transfer.setDefaultLabel("转办");
        transfer.setLabel("转办");
        transfer.setValue("T");
        transfer.setDisabled(false);
        actionMetaList.add(transfer);
        form.setActionMetaList(actionMetaList);
    }

}
